package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.dao.RichtextContentDao;
import io.renren.dto.RichtextContentDto;
import io.renren.entity.RichtextContentEntity;
import io.renren.service.RichtextContentService;
import io.renren.util.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("richtextContentService")
public class RichtextContentServiceImpl extends ServiceImpl<RichtextContentDao, RichtextContentEntity> implements RichtextContentService {
    /**
     * page、limit
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper wrapper = new QueryWrapper<RichtextContentEntity>();
        wrapper.eq("category_code", params.get("categoryCode"));
        IPage<RichtextContentEntity> page = this.page(
                new Query<RichtextContentEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 公司官网查询内容列表
     *
     * @param params
     * @return 返回列表数据
     */
    public Map<String, Object> querList(Map<String, Object> params) {
        PageUtils pageUtils = queryPage(params);
        Map<String, Object> map = new HashMap<>();
        if (pageUtils != null) {
            map.put("totalCount", pageUtils.getTotalCount());
            map.put("currPage", pageUtils.getCurrPage());
            map.put("totalPage", pageUtils.getTotalPage());
            if (pageUtils.getList() != null && !pageUtils.getList().isEmpty()) {
                List<RichtextContentDto> contentDtoList = new ArrayList<>();
                for (int index = 0; index < pageUtils.getList().size(); index++) {
                    RichtextContentEntity entity = (RichtextContentEntity) pageUtils.getList().get(index);
                    RichtextContentDto dto = new RichtextContentDto();
                    dto.setCategoryCode(entity.getCategoryCode());
                    dto.setCreateTime(entity.getCreateTime());
                    dto.setCreator(entity.getCreator());
                    dto.setId(entity.getId());
                    dto.setImageUrl(entity.getImageUrl());
                    dto.setTitle(entity.getTitle());
                    dto.setDes(entity.getDes());
                    dto.setProjectName(entity.getProjectName());
                    contentDtoList.add(dto);
                }
                map.put("list", contentDtoList);
            }
        }
        return map;
    }


}
